#define _CRT_SECURE_NO_WARNINGS   1
#include"List.h"

void Print_List(ListNode* head)
{
	ListNode* cur = head;
	printf("head<->");
	while (cur->next != head)
	{
		cur = cur->next;
		printf("%d",cur->val);
		if (cur->next != head)
			printf("<->");


	}
	
}


ListNode* CreatNewNode(ListNodeType x)
{
	ListNode* NewNode = (ListNode*)malloc(sizeof(ListNode));
	NewNode->val = x;
	return NewNode;
}

//void SLNodePushBack(ListNode* head, ListNodeType x) {
//
//	ListNode* NewNode = CreatNewNode(x);
//	ListNode* tail = head;
//	while (tail->next != head)
//	{
//		tail = tail->next;
//	}
//	tail->next =NewNode;
//	head->pre = NewNode;
//	NewNode->pre = tail;
//	NewNode->next = head;
//
//}
void SLNodePushBack(ListNode* head, ListNodeType x)
{
	ListNode* tail = head;
	while (tail->next != head)
	{
		tail = tail->next;
	}
	SLNodeInsert(tail, x);


}



//void SLNodePushFront(ListNode* head, ListNodeType x)
//{
//	ListNode* NewNode = CreatNewNode(x);
//	head->next->pre = NewNode;
//	NewNode->next = head->next;
//	head->next = NewNode;
//	NewNode -> pre = head;
//
//}
void SLNodePushFront(ListNode* head, ListNodeType x)
{

	SLNodeInsert(head, x);


}


//
//void SLNodePopback(ListNode* head)
//{
//	ListNode* tail = head;
//	while (tail->next != head)
//	{
//		tail = tail->next;
//	}
//	ListNode* Pre = tail->pre;
//	free(tail);
//	Pre->next = head;
//	head->pre = Pre;
//
//}
void SLNodePopback(ListNode* head) {
	ListNode* tail = head;
		while (tail->next != head)
		{
			tail = tail->next;
		}
		SLNodeEraser(tail);

}

 





//void SLNodePopFront(ListNode* head)
//{
//	assert(head->next!=head);
//	ListNode* Tmp = head->next->next;
//	free(head->next);
//	Tmp->pre = head;
//	head->next = Tmp;
//
//}
void SLNodePopFront(ListNode* head)
{
	ListNode* Tmp = head->next;
	SLNodeEraser(Tmp);

}


ListNode* FindNode(ListNode* head, ListNodeType x)
{
	ListNode* cur = head;
	while (cur->next != head)
	{
		if (cur->val == x)
			return cur;
		cur = cur->next;
	}
	printf("NULL");
	return NULL;

}


void SLNodeInsert( ListNode* pos, ListNodeType x)
{
	ListNode* Tmp = pos->next;
	ListNode* NewNode = CreatNewNode(x);
	Tmp ->pre = NewNode;
	NewNode->next = Tmp;
	pos->next = NewNode;
	NewNode->pre = pos;

}
void SLNodeEraser( ListNode* pos) {
	ListNode* Pre = pos->pre;
	ListNode* Next = pos->next;
	Pre->next = Next;
	Next->pre = Pre;
	free(pos);

}
